package com.shaomengjie.smjmvp.view.activity;

import android.animation.ArgbEvaluator;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.AppCompatButton;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.shaomengjie.smjmvp.R;
import com.shaomengjie.smjmvp.view.core.BaseActivity;

/**
 * Created by shaomengjie on 2017/4/19.
 */

public class GuidePageActivity extends BaseActivity implements View.OnClickListener {
    private int[] images = new int[]{R.mipmap.guide_new_one,R.mipmap.guide_new_two,R.mipmap.guide_new_three};
    private ViewPager mViewPager;
    private int bgColors[];
    private ImageView[] indicators;
    private int currentPosition;
    private ImageView imageView;
    private AppCompatButton buttonFinish;
    private ImageButton buttonPre;
    private ImageButton buttonNext;

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_guide_page,false);
    }

    @Override
    protected void initView() {
        buttonNext = (ImageButton) findViewById(R.id.imageButtonNext);
        buttonPre = (ImageButton) findViewById(R.id.imageButtonPre);
        buttonFinish = (AppCompatButton) findViewById(R.id.buttonFinish);
        indicators = new ImageView[] {
                (ImageView) findViewById(R.id.imageViewIndicator0),
                (ImageView) findViewById(R.id.imageViewIndicator1),
                (ImageView) findViewById(R.id.imageViewIndicator2)};
        mViewPager = (ViewPager) findViewById(R.id.container_viwePager);
        setClick();
    }

    private void setClick() {
        buttonPre.setOnClickListener(this);
        buttonNext.setOnClickListener(this);
        buttonFinish.setOnClickListener(this);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                int colorUpdate = (Integer) new ArgbEvaluator().evaluate(positionOffset, bgColors[position], bgColors[position == 2 ? position : position + 1]);
                mViewPager.setBackgroundColor(colorUpdate);
            }

            @Override
            public void onPageSelected(int position) {
                currentPosition = position;
                updateIndicators(position);
                mViewPager.setBackgroundColor(bgColors[position]);
                buttonPre.setVisibility(position == 0 ? View.GONE : View.VISIBLE);
                buttonNext.setVisibility(position == 2 ? View.GONE : View.VISIBLE);
                buttonFinish.setVisibility(position == 2 ? View.VISIBLE : View.GONE);
            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void updateIndicators(int position) {
        for (int i = 0; i < indicators.length; i++) {
            indicators[i].setBackgroundResource(
                    i == position ? R.drawable.onboarding_indicator_selected : R.drawable.onboarding_indicator_unselected
            );
        }
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        bgColors = new int[]{ContextCompat.getColor(this, R.color.guide_bg_one),
                ContextCompat.getColor(this, R.color.guide_bg_two),
                ContextCompat.getColor(this, R.color.guide_bg_three)};

        GuidePagerAdapter guidePagerAdapter = new GuidePagerAdapter();
        mViewPager.setAdapter(guidePagerAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageButtonPre:
                currentPosition -= 1;
                mViewPager.setCurrentItem(currentPosition, true);
                break;
            case R.id.imageButtonNext:
                currentPosition += 1;
                mViewPager.setCurrentItem(currentPosition, true);
                break;

            case R.id.buttonFinish:
                Intent intent = new Intent(GuidePageActivity.this,LoginActivity.class);
                startActivity(intent);
                finish();
                break;
            default:

                break;

        }
    }

    public class GuidePagerAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return images.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = LayoutInflater.from(getBaseContext()).inflate(R.layout.activity_guide_bg,null);
            imageView = (ImageView) view.findViewById(R.id.mGuideImageView);
            imageView.setImageResource(images[position]);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            View view = (View) object;
            ImageView imageView = (ImageView) view.findViewById(R.id.mGuideImageView);
            imageView.setImageBitmap(null);
            container.removeView(view);
        }
    }
}
